const Base = require('./base/base');

const
	base64_dict = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
	dict = '+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

/**
 * @extends base
 */
module.exports = class extends Base {
	encodeAction(param){
		let base64_code = Buffer.from(param.text,'binary').toString('base64').split('');
		let code = base64_code.map((c) => {
			let i = base64_dict.indexOf(c);
			return (i==-1)?c:dict[i];
		});
		return code.join('');
	}
	decodeAction(param){
		let code = param.text.split('');
		let base64_code = code.map((c) => {
			let i = dict.indexOf(c);
			return (i==-1)?c:base64_dict[i];
		});
		return Buffer.from(base64_code.join('').replace(/^X+/,''),'base64').toString();
	}
}
